<template>
  <el-dialog
    v-el-drag-dialog
    title="重置密码"
    :visible.sync="visible"
    width="400px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="80px">
      <el-form-item label="用户编码:" prop="userCode">
        <el-input v-model="temp.userCode" disabled />
      </el-form-item>
      <el-form-item label="新密码:" prop="newPwd">
        <el-input v-model="temp.newPwd" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="确认密码:" prop="checkPass">
        <el-input v-model="temp.checkPass" type="password" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { resetUserPwd } from '@/api'

  export default {
    name: 'reset-pwd',
    data() {
      const validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'))
        } else {
          if (this.temp.checkPass !== '') {
            this.$refs.dataForm.validateField('checkPass')
            const rg = /^[\w]{6,15}$/
            if (!rg.test(value)) {
              callback(new Error('新密码必须是6-15位的字母或数字'))
            } else {
              callback()
            }
          }
          callback()
        }
      }
      const validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.temp.newPwd) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        temp: {
          userCode: '',
          newPwd: '',
          checkPass: ''
        },
        rules: {
          newPwd: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          this.submit()
        })
      },
      async submit() {
        await resetUserPwd({
          userId: this.temp.userId,
          newPwd: this.temp.newPwd
        })
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '重置密码成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          userCode: '',
          newPwd: '',
          checkPass: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
